<template>
  <div class="student-container">

    <!-- 搜索 -->
    <el-form :inline="true" class="demo-form-inline" :model="studentValue">
      <el-form-item>
        <el-input placeholder="姓名或手机号" v-model="studentValue.search" />
      </el-form-item>

      <el-form-item>
        <el-date-picker value-format="x" @change="fn1" v-model="studentValue.timeValue" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" range-separator="--"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSearch">
          <el-icon style="font-weight:bolder; margin-right: 5px">
            <Search />
          </el-icon>
          搜索
        </el-button>
      </el-form-item>
      <el-button type="primary" @click="eExcelHandler">导出excel表格</el-button>
    </el-form>

    <!-- 表格数据 -->
    <el-table :data="tableDataFilter?.slice((currentPage - 1) * pageSize, currentPage * pageSize)" :border="true"
      style="width: 100%" class="student-data" :header-cell-style="{
        background: '#ecf7ff',
        color: '#666666',
        fontWeight: '600',
        fontSize: '16px',
      }" :cell-style="cellStyle">
      <el-table-column prop="id" label="序号" width="120" />
      <el-table-column prop="username" label="用户名" width="230" />
      <el-table-column prop="name" label="姓名" width="190" />
      <el-table-column prop="telephone" label="手机号" width="220" />
      <el-table-column prop="buy" label="已购课程" width="160" />
      <el-table-column prop="register" label="注册时间">
        <template #default="{ row }">
          <span>{{ dayjs.unix(row.register).format('YYYY-MM-DD') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页器 -->
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize" :current-page="currentPage"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" class="pagination-box" />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, computed } from 'vue'
import { Search } from '@element-plus/icons-vue';
import { getStudent } from '../../../api/user';
import dayjs from 'dayjs';
import exportFromJSON from 'export-from-json'


const tableDataFilter = ref([])
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const maxTime = ref(null)
const minTime = ref(null)

const total = computed(() => {
  return tableDataFilter.value?.length
})

// 日期选择
const studentValue = reactive({
  search: '',
  timeValue: '',
})

// 蓝色字体
const cellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (column.label === '已购课程') {
    return {
      color: '#1485fe'
    }
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
}

const handleCurrentChange = (val) => {
  currentPage.value = val
}

// 获取时间选择器选择的值
const fn1 = (timer) => {
  minTime.value = timer[0] / 1000
  maxTime.value = timer[1] / 1000
}

const fetchData = async () => {
  tableData.value = await getStudent()
  tableDataFilter.value = tableData.value
}

// 搜索事件
const toSearch = () => {
  if (!studentValue.search && !minTime.value) {
    tableDataFilter.value = tableData.value
    return
  }

  tableDataFilter.value = tableData.value.filter((item) => {
    return ((studentValue.search ? (item.name.includes(studentValue.search) || item.telephone.includes(studentValue.search)) : "false") && (minTime.value ? (item.register >= minTime.value && item.register <= maxTime.value) : "false"))
  })
}

onMounted(() => {
  fetchData()
})


// 表格导出
const eExcelHandler = () =>{
  const data = tableData.value
  const fileName = '学生表格数据'
  const exportType = 'xls'
  exportFromJSON({data,fileName,exportType})
}



</script>

<style lang="scss" scoped>
.demo-form-inline {
  display: flex;
}

.student-container {
  min-height: 600px;
  height: 100%;
  padding: 32px;
  box-sizing: border-box;
}

.student-data {
  width: 100%;
  height: 100%;
  border: 1px solid #eeeeee;
}

:deep(.el-table .el-table__cell) {
  text-align: center;
}

.pagination-box {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>